import { createPinia, defineStore } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate'

const pinia = createPinia()
//@ts-ignore
pinia.use(
    createPersistedState({

        storage: {
            getItem: uni.getStorageSync,
            setItem: uni.setStorageSync,
        },
    }),
)
//导出pinia实例
export default pinia

//定义一个小匣子
const useStore = defineStore('pinche-store', {
    state: () => {
        return {

            name: "小佩奇",
            token: "",
            traceId: "",


        }
    },
    getters: {

        globalTraceId: (state) => {
            state.traceId = state.traceId == "" ? "" : state.traceId
            return state.traceId
        }, // 计算属性

    },
    // 小程序端配置
    persist: {
        storage: {
            getItem(key) {
                return uni.getStorageSync(key)
            },
            setItem(key, value) {
                uni.setStorageSync(key, value)
            },
        },
    },

})


//导出小匣子
export { useStore }
